import http from '@/api/http.js'

/**
 * @param {Number} id 订单id
 * @param {Number} times 请求次数
 * @description 检查订单是否支付成功
  */
const checkOrderPay = async function(id, ordersn, times){
	// 获取订单详情
	let params = {
			method: 'api/v2/order/get_status',
			id
		}
	uni.showLoading({
		title: '支付确认中...'
	})
	
	let res = await http(params)
	
	times--
	
	if (res.status) {
		
		const {status, ispay} = res.data
		
		// 判断是否是已经支付成功
		if(status == 2 || (status == 0 && ispay==1)){
			
			//小程序支付成功前往支付成功页
			uni.redirectTo({
				url:`/pages/order/paysuccess?ordersn=${ordersn}&orderid=${id}`
			})
			uni.hideLoading()
		}else{
			if(times >= 0){
				setTimeout(() => {
					checkOrderPay(id, ordersn, times)
				}, 1000)
			}else{
				// 前往支付异常页面
				uni.redirectTo({
					url: `/pages/order/payfail?ordersn=${ordersn}&orderid=${id}`
				})
			}
		}
	} else {
		uni.showToast({
			title: res.msg,
			icon: 'none'
		})
	}
}
export default checkOrderPay
